package com.hqhy.like.stock.controller;

import com.hqhy.like.common.web.JsonResult;
import com.hqhy.like.stock.pojo.param.CategoryAddParam;
import com.hqhy.like.stock.pojo.vo.CategoryListItemVO;
import com.hqhy.like.stock.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stock/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增类别
     */
    @PostMapping("/insert")
    public JsonResult insert(@Valid CategoryAddParam categoryAddParam) {
        log.debug("开始处理【新增类别】的请求，参数：{}", categoryAddParam);
        categoryService.insert(categoryAddParam);
        return JsonResult.ok();
    }

    /**
     * 根据id删除类别
     */
    @PostMapping("/delete")
    public JsonResult deleteById(Long id) {
        log.debug("开始处理【根据id删除类别】的请求，参数：{}", id);
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 根据id修改类别
     */
    public JsonResult updateById(CategoryListItemVO categoryListItemVO) {
        log.debug("开始处理【根据id修改类别】的请求，参数：{}", categoryListItemVO);
        categoryService.updateById(categoryListItemVO);
        return JsonResult.ok();
    }

    /**
     * 查询类别列表
     */
    @GetMapping("/list")
    public JsonResult list() {
        log.debug("开始处理【查询类别列表】的请求");
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }
}
